﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;
using DevExpress.Utils;
using DevExpress.XtraGrid.Views.Grid;
using QuanLyKho.PhongCat;
using QuanLyKho.PhongLenh;

namespace QuanLyKho.Control
{
    public partial class Phongcat_Sanphamcat : UserControl
    {
        public Phongcat_Sanphamcat()
        {
            InitializeComponent();
            Loaddata();
            SetPropertiesGrid();
            SetPropertiesColumns();
        }

        public void Loaddata()
        {
            gridLenh.DataSource = DataAll.SelectAll("LenhSanXuat inner join PhongMau_SanPham On LenhSanXuat.IdSanPham = PhongMau_SanPham.IdSanPham",
                "LenhSanXuat.[ID],PhongMau_SanPham.[MaHang] as MaSanPham,LenhSanXuat.[IdSanPham],[NgayLapLenh],[NguoiLapLenh],LenhSanXuat.[SoLuong],[NgayGuiLenh],[NgaySuaLenh],[NgayDuyetLenh],LenhSanXuat.[TrangThai],[NgayChungTu],[NgayHoachToan],LenhSanXuat.[SoChungTu],LenhSanXuat.[GhiChu],[KhoDen],[KhoDi],[SXTuNgay],[SXDenNgay]",
                "WHERE LenhSanXuat.TrangThai = " + Convert.ToInt32(TrangThai.LenhSanXuat_CapNhatKho) + " OR LenhSanXuat.TrangThai = " + Convert.ToInt32(TrangThai.LenhSanXuat_DaNhanLenh));

            gridPhongCat.DataSource = DataAll.SelectAll("Cat inner join PhongMau_SanPham on Cat.IdSanPham = PhongMau_SanPham.IdSanPham",
                    "PhongMau_SanPham.[MaHang] as MaSanPham,PhongMau_SanPham.[LoaiHang],PhongMau_SanPham.[LanSanXuat],PhongMau_SanPham.[LenhSanXuat],Cat.[ID],Cat.[IdSanPham],Cat.[NgayLapLenh],Cat.[NguoiCapNhatLenh],Cat.[SoLuong],Cat.[NgayGuiLenh],Cat.[NgaySuaLenh],Cat.[TrangThai],Cat.[NgayChungTu],Cat.[NgayHoachToan],Cat.[SoChungTu],Cat.[GhiChu],Cat.[KhoDen],Cat.[KhoDi],Cat.[SXTuNgay],Cat.[SXDenNgay]", "");
        }

        public void SetPropertiesGrid()
        {
            gvPhongCat.BestFitColumns();
            gvPhongCat.FocusRectStyle = DrawFocusRectStyle.RowFullFocus;
            gvPhongCat.Appearance.SelectedRow.BackColor = Color.Empty;
            gvPhongCat.Appearance.SelectedRow.Options.UseBackColor = true;
            gvPhongCat.OptionsView.ShowGroupPanel = false;
            gvPhongCat.OptionsView.ShowFooter = true;
            gvPhongCat.OptionsFind.AlwaysVisible = true;
            gvPhongCat.Columns["ID"].Visible = false;

            gvLenh.BestFitColumns();
            gvLenh.FocusRectStyle = DrawFocusRectStyle.RowFullFocus;
            gvLenh.OptionsBehavior.Editable = false;
            gvLenh.OptionsSelection.MultiSelect = true;
            gvLenh.OptionsView.ShowGroupPanel = false;
            gvLenh.OptionsView.ShowFooter = true;
            gvLenh.OptionsFind.AlwaysVisible = true;
            gvLenh.Columns["ID"].Visible = false;
        }

        public void SetPropertiesColumns()
        {
            gcMayKhoan.Width = 70;
            gcGiaCong.Width = 50;
            gcMayKhoan.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcGiaCong.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcMaSanPham.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcNgayLapLenh.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            //gcNguoiLapLenh.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcSoLuong.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcNgaySuaLenh.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcTrangThai.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcNgayChungTu.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcNgayHoachToan.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcSoChungTu.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcGhiChu.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            //gcS.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            //gcM.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            //gcL.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            //gcXL.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            //gcTongCan.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            //gcDMTT.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            //gcSLCan.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
        }

        private void gvPhongCat_DoubleClick(object sender, EventArgs e)
        {
            if (gvPhongCat.RowCount > 0)
            {
                string a = gvPhongCat.GetRowCellValue(gvPhongCat.FocusedRowHandle, "TrangThai").ToString();
                string b = Convert.ToInt32(TrangThai.Cat_TaoMoi).ToString();
                if (a == b)
                {
                    if (gvPhongCat.FocusedRowHandle >= 0)
                    {
                        TaoSanPhamCat.MaID = gvPhongCat.Columns["ID"].View.GetRowCellDisplayText(gvPhongCat.FocusedRowHandle, "ID");
                        var lss = new TaoSanPhamCat();
                        lss.ShowDialog();
                        Loaddata();
                        TaoSanPhamCat.MaID = "0";
                    }
                }
            }
        }

        private void gvLenh_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "TrangThai")
            {
                e.DisplayText = Common.HienThiTrangThai(Convert.ToInt32(e.Value));
            }
        }

        private void btGuiLenh_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var dataview = (DataView)gvPhongCat.DataSource;
            string a = gvPhongCat.GetRowCellValue(gvPhongCat.FocusedRowHandle, "TrangThai").ToString();
            string b = Convert.ToInt32(TrangThai.Cat_TaoMoi).ToString();
            if (a == b)
            {
                DialogResult dialogResult = MessageBox.Show("Bạn có chắc chắn muốn gửi.", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dialogResult == DialogResult.Yes)
                {
                    string message = DataAll.Update("Cat", "TrangThai =" + Convert.ToInt32(TrangThai.Cat_GuiGiaCong) + ",NgayGuiLenh ='" + DateTime.Now.ToString("MM/dd/yyyy") + "' WHERE ID =" + dataview[gvPhongCat.FocusedRowHandle]["ID"]);

                    MessageBox.Show(message.Equals("success") ? "Gửi lệnh thành công" : message, "Message",
                                        MessageBoxButtons.OK,
                                        message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                    DataAll.InsertAll("HanhTrinh", dataview[gvPhongCat.FocusedRowHandle]["MaSanPham"]
                            + "," + Convert.ToInt32(PhongBan.PhongCat)
                            + "," + dataview[gvPhongCat.FocusedRowHandle]["SoChungTu"]
                            + "," + Convert.ToInt32(TrangThai.Cat_GuiGiaCong)
                            + "," + Main.UserID
                            + "," + DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff")
                            + "," + dataview[gvPhongCat.FocusedRowHandle]["SoLuong"]
                            + "," + Convert.ToDateTime(dataview[gvPhongCat.FocusedRowHandle]["SXTuNgay"].ToString()).ToString("MM/dd/yyyy")
                            + "," + Convert.ToDateTime(dataview[gvPhongCat.FocusedRowHandle]["SXDenNgay"].ToString()).ToString("MM/dd/yyyy"));
                    Loaddata();
                }
            }
        }

        private void gvLenh_DoubleClick(object sender, EventArgs e)
        {
            if (gvLenh.RowCount > 0)
            {
                var dataview = (DataView)gvLenh.DataSource;
                var data = DataAll.SelectAll("HanhTrinh", "*", " WHERE SoChungTu='" + dataview[gvLenh.FocusedRowHandle]["SoChungTu"]
                    + "' AND TrangThai=" + Convert.ToInt32(TrangThai.LenhSanXuat_DaNhanLenh));
                if (data.Rows.Count <= 0)
                {
                    DataAll.InsertAll("HanhTrinh", dataview[gvLenh.FocusedRowHandle]["MaSanPham"]
                                + "," + Convert.ToInt32(PhongBan.PhongCat)
                                + "," + dataview[gvLenh.FocusedRowHandle]["SoChungTu"]
                                + "," + Convert.ToInt32(TrangThai.LenhSanXuat_DaNhanLenh)
                                + "," + Main.UserID
                                + "," + DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff")
                                + "," + dataview[gvLenh.FocusedRowHandle]["SoLuong"]
                                + "," + Convert.ToDateTime(dataview[gvLenh.FocusedRowHandle]["SXTuNgay"].ToString()).ToString("MM/dd/yyyy")
                                + "," + Convert.ToDateTime(dataview[gvLenh.FocusedRowHandle]["SXDenNgay"].ToString()).ToString("MM/dd/yyyy"));
                }
                TaoSanPhamCat.IdLenh = gvLenh.Columns["ID"].View.GetRowCellDisplayText(gvLenh.FocusedRowHandle, "ID");
                var lss = new TaoSanPhamCat();
                lss.ShowDialog();
                Loaddata();
                TaoSanPhamCat.IdLenh = "0";
            }
        }

        private void btMayKhoan_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var dataview = (DataView)gvPhongCat.DataSource;
            string a = gvPhongCat.GetRowCellValue(gvPhongCat.FocusedRowHandle, "TrangThai").ToString();
            string b = Convert.ToInt32(TrangThai.Cat_TaoMoi).ToString();
            if (a == b)
            {
                DialogResult dialogResult = MessageBox.Show("Bạn có chắc chắn muốn gửi.", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dialogResult == DialogResult.Yes)
                {
                    string message = DataAll.Update("Cat", "TrangThai =" + Convert.ToInt32(TrangThai.Cat_GuiMayKhoan) + ",NgayGuiLenh ='" + DateTime.Now.ToString("MM/dd/yyyy") + "' WHERE ID =" + dataview[gvPhongCat.FocusedRowHandle]["ID"]);

                    MessageBox.Show(message.Equals("success") ? "Gửi thành công" : message, "Message",
                                        MessageBoxButtons.OK,
                                        message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                    DataAll.InsertAll("HanhTrinh", dataview[gvPhongCat.FocusedRowHandle]["MaSanPham"]
                            + "," + Convert.ToInt32(PhongBan.PhongCat)
                            + "," + dataview[gvPhongCat.FocusedRowHandle]["SoChungTu"]
                            + "," + Convert.ToInt32(TrangThai.Cat_GuiMayKhoan)
                            + "," + Main.UserID
                            + "," + DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff")
                            + "," + dataview[gvPhongCat.FocusedRowHandle]["SoLuong"]
                            + "," + Convert.ToDateTime(dataview[gvPhongCat.FocusedRowHandle]["SXTuNgay"].ToString()).ToString("MM/dd/yyyy")
                            + "," + Convert.ToDateTime(dataview[gvPhongCat.FocusedRowHandle]["SXDenNgay"].ToString()).ToString("MM/dd/yyyy"));
                    Loaddata();
                }
            }
        }

        private void gvPhongCat_RowStyle(object sender, RowStyleEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                GridView View = sender as GridView;
                string trangthai = View.GetRowCellDisplayText(e.RowHandle, View.Columns["TrangThai"]);
                if (trangthai == "Phòng cắt tạo mới")
                {
                    e.Appearance.BackColor = Color.Empty;
                }
                else if (trangthai == "Đã gửi phòng gia công" || trangthai == "Đã gửi phòng may khoán")
                {
                    e.Appearance.BackColor = Color.SandyBrown;
                }
                else
                {
                    e.Appearance.BackColor = Color.DarkSeaGreen;
                }
            }
        }

        private void gvLenh_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            if (e.Column.FieldName == "SXDenNgay")
            {
                TimeSpan songay = Convert.ToDateTime(e.CellValue) - DateTime.Now;
                if (0 < songay.Days && songay.Days < 5)
                {
                    e.Appearance.BackColor = Color.PaleGoldenrod;
                }
                if (songay.Days < 0)
                {
                    e.Appearance.BackColor = Color.DarkGray;
                }
            }
        }

        private void btnHuy_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var dataview = (DataView)gvPhongCat.DataSource;
            string a = gvPhongCat.GetRowCellValue(gvPhongCat.FocusedRowHandle, "TrangThai").ToString();
            string b = Convert.ToInt32(TrangThai.Cat_TaoMoi).ToString();
            if (a == b)
            {
                DialogResult dialogResult = MessageBox.Show("Bạn có chắc chắn muốn hủy ?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dialogResult == DialogResult.Yes)
                {
                    string message = DataAll.Update("LenhSanXuat", "TrangThai = " + Convert.ToInt32(TrangThai.LenhSanXuat_CapNhatKho) + " WHERE SoChungTu ='" + dataview[gvPhongCat.FocusedRowHandle]["SoChungTu"] +"'");
                    if (message.Equals("success"))
                    {
                        DataAll.Delete("Cat","ID =" + dataview[gvPhongCat.FocusedRowHandle]["ID"]);
                    }
                    MessageBox.Show(message.Equals("success") ? "Hủy thành công" : message, "Message",
                                       MessageBoxButtons.OK,
                                       message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                }
            }
            Loaddata();
        }
    }
}
